// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

import {GameVar, GlobalObject} from "../game/GameVar";

const {ccclass, property} = cc._decorator;

@ccclass
export default class VoiceSource extends cc.Component {

  @property({type: cc.AudioClip})
  menuBgm: cc.AudioClip = null;

  @property({type: cc.AudioClip})
  battleBgm: cc.AudioClip = null;

  @property({type: cc.AudioClip})
  hit: cc.AudioClip = null;

  @property({type: cc.AudioClip})
  acc: cc.AudioClip = null;

  @property({type: cc.AudioClip})
  settlement: cc.AudioClip = null;

  @property({type: cc.AudioClip})
  died: cc.AudioClip = null;

  @property({type: cc.AudioClip})
  switch: cc.AudioClip = null;

  @property({type: cc.AudioClip})
  revive: cc.AudioClip = null;

  @property({type: cc.AudioClip})
  compose: cc.AudioClip = null;

  @property({type: cc.AudioClip})
  whirlpool: cc.AudioClip = null;

  @property({type: cc.AudioClip})
  bubble: cc.AudioClip = null;

  @property({type: cc.AudioClip})
  rotate: cc.AudioClip = null;

  @property({type: cc.AudioClip})
  click: cc.AudioClip = null;


  // @property({type: cc.AudioClip})
  // public menuBgm: cc.AudioClip = null;

  private effVolume = 1;
  private bgmVolume = 1;
  private accAudioId = -1;
  private bgmAudioId = -1;

  onLoad() {
    GlobalObject.voiceSource = this;
    cc.game.addPersistRootNode(this.node);
    /*
    MARK:h5写入任何类型值都会返回字符串，微信返回正确的类型，为了保证一致，写入字符串
    */
    this.bgmVolume = (cc.sys.localStorage.getItem('muteBgm') === 'true') ? 0 : 1;
    this.effVolume = (cc.sys.localStorage.getItem('muteEffect') === 'true') ? 0 : 1;
  }


  get muteBgm(): boolean {
    return this.bgmVolume === 0;
  }

  set muteBgm(flag: boolean) {
    this.bgmVolume = flag ? 0 : 1;
    (this.bgmAudioId !== -1) && cc.audioEngine.setVolume(this.bgmAudioId, this.bgmVolume);
    cc.sys.localStorage.setItem('muteBgm', flag.toString());
  }

  get muteEffect(): boolean {
    return this.effVolume === 0;
  }

  set muteEffect(flag: boolean) {
    this.effVolume = flag ? 0 : 1;
    (this.accAudioId !== -1) && cc.audioEngine.setVolume(this.accAudioId, this.effVolume);
    cc.sys.localStorage.setItem('muteEffect', flag.toString());
  }


  playMenuBgm() {
    cc.audioEngine.stopAll()
    this.bgmAudioId = cc.audioEngine.play(this.menuBgm, true, this.bgmVolume);
  }

  playBattleBgm() {
    cc.audioEngine.stopAll()
    this.bgmAudioId = cc.audioEngine.play(this.battleBgm, true, this.bgmVolume);
  }

  playHit() {
    cc.audioEngine.play(this.hit, false, this.effVolume);
  }


  playAcc() {
    if (this.accAudioId == -1) {
      this.accAudioId = cc.audioEngine.play(this.acc, true, this.effVolume);
    }
  }

  stopAcc() {
    if (this.accAudioId != -1) {
      cc.audioEngine.stop(this.accAudioId);
      this.accAudioId = -1;
    }
  }

  playSettlement() {
    cc.audioEngine.play(this.settlement, false, this.effVolume);
  }

  playDied() {
    cc.audioEngine.play(this.died, false, this.effVolume);
  }

  playSwitch() {
    cc.audioEngine.play(this.switch, false, this.effVolume);
  }

  playCompose() {
    cc.audioEngine.play(this.compose, false, this.effVolume);
  }

  playRevive() {
    cc.audioEngine.play(this.revive, false, this.effVolume);
  }

  playWhirlpool() {
    cc.audioEngine.play(this.whirlpool, false, this.effVolume)
  }

  playBubble() {
    cc.audioEngine.play(this.bubble, false, this.effVolume)
  }

  playRotate() {
    cc.audioEngine.play(this.rotate, false, this.effVolume)
  }

  playClick() {
    cc.audioEngine.play(this.click, false, this.effVolume)
  }


}
